# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=pyrex
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.9
pkgrel=1
pkgdesc="Language for writing Python extension modules (mingw-w64)"
arch=('any')
license=('APACHE')
url="https://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/"
depends=("${MINGW_PACKAGE_PREFIX}-python2")
makedepends=("p7zip")
source=("${_realname}-${pkgver}.tar.gz"::"https://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/Pyrex-${pkgver}.tar.gz")
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('5f87df06831d0b3412eb4bc9d3fc2ee7bfae1b913d7da8c23ab2bf5699fb6b50')

prepare() {
  cd ${srcdir}
  7z x -o${srcdir} -r -aoa "${_realname}-${pkgver}.tar.gz" && 7z x -o${srcdir} -r -aoa "Pyrex-${pkgver}.tar"
  for pver in 2; do 
    rm -rf python${pver}-build-${CARCH} | true
    cp -r "${_realname}-${pkgver}" "python${pver}-build-${CARCH}"
  done
}

build() {
  cd "${srcdir}"
  for pver in 2; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

package() {

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${_f}
  done
}
